////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// FIGURE 6 Bayesian Benchmark in NoGroup Treatment


See R files that generates this figure

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// FIGURE 7 Best Linear Fit vs. Best Fractional Polynomial Fit

use "$data/eoydata_late.dta", clear
keep if filteredsample ==1
gen dummy = 1
egen weight = sum(dummy), by(value highgroup treatmentname)
egen mguess = mean(guess), by(value highgroup treatmentname)

gen lbound = 30
gen hbound = 70

preserve 
keep if treatmentname =="Baseline"
tw scatter mguess value [w=weight] if value >lbound & value < hbound & highgroup ==1, msize(*0.15) mcolor(orange*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) lpattern(dash) ///
|| scatter mguess value [w=weight] if value >lbound & value < hbound & highgroup ==0, msize(*0.15) mcolor(green*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) lpattern(dash) ///
xtitle("True Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
 title("Baseline", size(medsmall) ) ///
  saving("$tempgraph/bl", replace) 

  restore
  preserve 
keep if treatmentname =="SignalFirst"
tw scatter mguess value [w=weight] if value >lbound & value < hbound & highgroup ==1, msize(*0.15) mcolor(orange*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) lpattern(dash) ///
|| scatter mguess value [w=weight] if value >lbound & value < hbound & highgroup ==0, msize(*0.15) mcolor(green*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) lpattern(dash) ///
xtitle("True Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
 title("SignalFirst", size(medsmall) ) ///
  saving("$tempgraph/sf", replace) 

  restore
  preserve 
keep if treatmentname =="OneGroup"
tw scatter mguess value [w=weight] if value >lbound & value < hbound & highgroup ==1, msize(*0.15) mcolor(orange*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) lpattern(dash) ///
|| scatter mguess value [w=weight] if value >lbound & value < hbound & highgroup ==0, msize(*0.15) mcolor(green*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) lpattern(dash) ///
xtitle("True Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
 title("OneGroup", size(medsmall) ) ///
  saving("$tempgraph/og", replace) 
  
  
  restore
  preserve 
keep if treatmentname =="NoGroup"
tw scatter mguess value if value >lbound & value < hbound & highgroup ==1, mcolor(orange*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==1, lcolor(dkorange) lpattern(dash) ///
|| scatter mguess value if value >lbound & value < hbound & highgroup ==0, mcolor(green*0.5) msymbol(oh) ///
|| lfit guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) ///
|| fpfit  guess value if value >lbound & value < hbound & highgroup ==0, lcolor(dkgreen) lpattern(dash) ///
xtitle("True Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
 title("NoGroup", size(medsmall) ) ///
  saving("$tempgraph/ng", replace) 
  
  
    
 graph combine "$tempgraph/bl"  "$tempgraph/ng" , graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(1) title("") ///
title("", size(medium)) ///
  note("" ///
 , size(tiny))  
graph export "$text/files/guessbyvalue_poly_blng_late.pdf", replace

   
graph combine "$tempgraph/og"  "$tempgraph/sf" , graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(1) title("") ///
title("", size(medium)) ///
  note("Dots average assessment. Solid lines depict best linear fit by group and treatment." ///
  "Dashed lines depict best fractional polynomial fit." ///
 , size(vsmall))  
graph export "$text/files/guessbyvalue_poly_ogsf_late.pdf", replace
	
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// FIGURE 8 Best Linear Fit and Binned Scatter Plots
use "$data/eoydata_late.dta", clear
keep if filteredsample ==1


preserve 
keep if treatmentname =="Baseline"

binsreg guess value, ci(1 1) cb(1 1) nbins(10) ///
polyreg(1) ///
  line(3 3) ///
by(highgroup)  bycolors(dkgreen dkorange)  bysymbols(circle circle) plotxrange(30 70) ///
legend(off) ylabel(30(10)70, labsize(small)) xlabel(30(10)70, labsize(small)) plotregion(style(none) ) ///
	   scheme(s1mono) ///
	   xtitle("Value", size(small)) ytitle("Assesment", size(small))  ///
	   title("Baseline", size(medsmall) ) ///
  saving("$tempgraph/bl", replace) 
  
restore
preserve 
keep if treatmentname =="SignalFirst"
binsreg guess value, ci(1 1) cb(1 1) nbins(10) ///
polyreg(1) ///
by(highgroup)  bycolors(dkgreen dkorange) bysymbols(circle circle) plotxrange(30 70) ///
legend(off) ylabel(30(10)70, labsize(small)) xlabel(30(10)70, labsize(small)) plotregion(style(none) ) ///
	   scheme(s1mono) ///
	   xtitle("Value", size(small)) ytitle("Assesment", size(small))  ///
	   title("SignalFirst", size(medsmall) ) ///
  saving("$tempgraph/sf", replace) 
  
  
  restore
preserve 
keep if treatmentname =="OneGroup"
binsreg guess value, ci(1 1) cb(1 1) nbins(10) ///
polyreg(1) ///
by(highgroup)  bycolors(dkgreen dkorange) bysymbols(circle circle) plotxrange(30 70) ///
legend(off) ylabel(30(10)70, labsize(small)) xlabel(30(10)70, labsize(small)) plotregion(style(none) ) ///
	   scheme(s1mono) ///
	   xtitle("Value", size(small)) ytitle("Assesment", size(small))  ///
	   title("OneGroup", size(medsmall) ) ///
  saving("$tempgraph/og", replace) 
  
  
  restore
preserve 
keep if treatmentname =="NoGroup"
binsreg guess value, ci(1 1) cb(1 1) nbins(10) ///
polyreg(1) ///
by(highgroup)  bycolors(dkgreen dkorange) bysymbols(circle circle) plotxrange(30 70) ///
legend(off) ylabel(30(10)70, labsize(small)) xlabel(30(10)70, labsize(small)) plotregion(style(none) ) ///
	   scheme(s1mono) ///
	   xtitle("Value", size(small)) ytitle("Assesment", size(small))  ///
	   title("NoGroup", size(medsmall) ) ///
  saving("$tempgraph/ng", replace) 
  
  
         graph combine "$tempgraph/bl"  "$tempgraph/ng" , graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(1) title("") ///
title("", size(medium)) ///
  note("" ///
 , size(tiny))  
graph export "$text/files/guessbyvalue_binsreg_blng_late.pdf", replace

   
       graph combine "$tempgraph/og"  "$tempgraph/sf" , graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(1) title("") ///
title("", size(medium)) ///
  note("Dots show binned scatter plots depicting nonparamatric estimates of guess given value in each bin. Green and Orange lines depict best linear fit by group and treatment." ///
  "Subjects with MSE > 200 excluded." ///
 , size(tiny))  
graph export "$text/files/guessbyvalue_binsreg_ogsf_late.pdf", replace
	
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// FIGURE 9 Estimates of Bayesian Weight vs. Weight on Prior by Group and Treatment 	

************************************
*optimal vs. actual alpha by treatment. 

use "$data/eoydata_late.dta", clear
sort id highgroup round
bysort id highgroup: gen round_bygroup = _n
keep if round_bygroup == 1

gen lb = -0.2
gen hb = 0.6
gen adiff = -(alpha_i - alpha_opt_i)
gen adiffdummy = adiff<0
bysort treatmentname: summarize adiffdummy



preserve

keep if treatmentname == "SignalFirst"
sort adiff highgroup
egen mfoo = median(adiff), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)

local mfool = bazl
local mfooh = bazh

cumul adiff if highgroup ==1, gen(cu_h)
cumul adiff if highgroup ==0, gen(cu_l)


line cu_h adiff if highgroup ==1 & adiff >lb & adiff < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l adiff if highgroup ==0 & adiff >lb & adiff < hb, sort(cu_l) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
	t1title("SignalFirst" , size(medium)) xtitle("{&omega}{sup:Bay}- {&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)0.6) ///
	ytitle("Cdf", size(medium)) ///
	  saving("$tempgraph/sf", replace) 
	  
	 * legend(order(1 "High-mean" 2 "Low-mean") size(small) rows(2) ring(0) pos(5))  ///	
	
	  
restore
preserve
keep if treatmentname == "Baseline"
sort adiff highgroup
egen mfoo = median(adiff), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)

local mfool = bazl
local mfooh = bazh

cumul adiff if highgroup ==1, gen(cu_h)
cumul adiff if highgroup ==0, gen(cu_l)


line cu_h adiff if highgroup ==1 & adiff >lb & adiff < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l adiff if highgroup ==0 & adiff >lb & adiff < hb, sort(cu_l) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
	t1title("Baseline" , size(medium)) xtitle("{&omega}{sup:Bay}-{&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)0.6) ///
	ytitle("Cdf", size(medium)) ///
	  saving("$tempgraph/bl", replace) 
	  
restore
preserve
keep if treatmentname == "OneGroup"
sort adiff highgroup
egen mfoo = median(adiff), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)

local mfool = bazl
local mfooh = bazh

cumul adiff if highgroup ==1, gen(cu_h)
cumul adiff if highgroup ==0, gen(cu_l)


line cu_h adiff if highgroup ==1 & adiff >lb & adiff < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l adiff if highgroup ==0 & adiff >lb & adiff < hb, sort(cu_l) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
	t1title("OneGroup" , size(medium)) xtitle("{&omega}{sup:Bay}- {&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)0.6) ///
	ytitle("Cdf", size(medium)) ///
	  saving("$tempgraph/og", replace) 
	  
	  restore
preserve
keep if treatmentname == "NoGroup"
sort adiff highgroup
egen mfoo = median(adiff), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)

local mfool = bazl
local mfooh = bazh

cumul adiff if highgroup ==1, gen(cu_h)
cumul adiff if highgroup ==0, gen(cu_l)


line cu_h adiff if highgroup ==1 & adiff >lb & adiff < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l adiff if highgroup ==0 & adiff >lb & adiff < hb, sort(cu_l) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
	t1title("NoGroup" , size(medium)) xtitle("{&omega}{sup:Bay} - {&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)0.6) ///
	ytitle("Cdf", size(medium)) ///
	  saving("$tempgraph/ng", replace) 
	  
	  
graph combine "$tempgraph/bl" "$tempgraph/ng" "$tempgraph/og" "$tempgraph/sf", graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(2) title("") ///
title("", size(medium)) 


graph export "$text/files/cdfalphavsoptalpha_late.pdf", replace


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// FIGURE 10 Distribution of Individual Estimates of $\Delta_h  - \Delta_l$ by Treatment
	  ************************************
use "$data/eoydata_late.dta", clear
gen delta_i = bias_i/(1- alpha_i)
gen foo = delta_i
replace foo = . if highgroup ==1
egen delta_i_l = max(foo), by(id)
drop foo
gen foo = delta_i - delta_i_l
replace foo = . if highgroup ==0
egen baz = max(foo), by(id)
replace bdelta_i = baz
keep if round == 38
gen lb = -5
gen hb = 20
sort bdelta_i treatmentname

cumul bdelta_i if treatmentname == "SignalFirst", gen(cu_sf)
cumul bdelta_i if treatmentname == "Baseline", gen(cu_bl)
cumul bdelta_i if treatmentname == "NoGroup", gen(cu_ng)


line cu_bl bdelta_i if bdelta_i >lb & bdelta_i < hb, sort(cu_bl) lcolor(black) ///
|| line cu_ng bdelta_i if bdelta_i >lb & bdelta_i < hb, sort(cu_ng) lcolor(gray)  ///
|| line cu_sf bdelta_i if bdelta_i >lb & bdelta_i < hb, sort(cu_sf) lcolor(gray) lpattern(dash) ///
	t1title("" , size(medium)) xtitle("{&Delta}{sub:h} - {&Delta}{sub:l}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(order(1 "Baseline" 2 "NoGroup" 3 "SignalFirst") size(small) rows(3) ring(0) pos(5))  ///
	xlabel(-5(3)20) ///
	ytitle("Cdf", size(medium)) 
graph export "$text/files/cdfofdeltadiff_late.pdf", replace




////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// FIGURE 11 Distribution of Individual Estimates of Weight on Prior by Group and Treatment

************************************
*optimal vs. actual alpha by treatment. 

use "$data/eoydata_late.dta", clear
sort id highgroup round
bysort id highgroup: gen round_bygroup = _n
keep if round_bygroup == 1

reg var_signal_i d_sf d_og d_ng if highgroup ==1
reg var_signal_i d_sf d_og d_ng if highgroup ==0

gen lb = -0.2
gen hb = 1

replace alpha_i = alpha_i
replace alpha_opt_i = alpha_opt_i

preserve
keep if highgroup ==0
keep if treatmentname == "OneGroup" | treatmentname == "Baseline"
ksmirnov alpha_opt_i, by(treatment)

restore
preserve

keep if treatmentname == "SignalFirst"
sort alpha_i highgroup
egen mfoo = mean(alpha_i), by(highgroup)
egen mfooopt = mean(alpha_opt_i), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
gen xxoh = mfooopt
gen xxol = mfooopt
replace xxoh = . if highgroup ==0
replace xxol = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)
egen bazol = max(xxol)
egen bazoh = max(xxoh)

local mfool = bazl
local mfooh = bazh
local mfoolopt = bazol
local mfoohopt = bazoh


cumul alpha_i if highgroup ==1, gen(cu_h)
cumul alpha_i if highgroup ==0, gen(cu_l)
sort alpha_opt_i highgroup
cumul alpha_opt_i if highgroup ==1, gen(cu_h_o)
cumul alpha_opt_i if highgroup ==0, gen(cu_l_o)

line cu_h alpha_i if highgroup ==1 & alpha_i >lb & alpha_i < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l alpha_i if highgroup ==0 & alpha_i >lb & alpha_i < hb, sort(cu_l) lcolor(dkgreen)	///
	|| line cu_h_o alpha_opt_i if highgroup ==1 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_h_o) lpattern(dash) lcolor(dkorange)	///
	|| line cu_l_o alpha_opt_i if highgroup ==0 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_l_o) lpattern(dash) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
					xline(`mfoolopt', lcolor(dkgreen) lpattern(dash)) ///
		xline(`mfoohopt', lcolor(dkorange) lpattern(dash)) ///
	t1title("SignalFirst" , size(medium)) xtitle("{&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)1) ///
	ytitle("Cdf", size(small)) ///
	  saving("$tempgraph/sf", replace) 
	  
	 * legend(order(1 "High-mean" 2 "Low-mean") size(small) rows(2) ring(0) pos(5))  ///	
	
	  
restore
preserve
keep if treatmentname == "Baseline"
sort alpha_i highgroup
egen mfoo = mean(alpha_i), by(highgroup)
egen mfooopt = mean(alpha_opt_i), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
gen xxoh = mfooopt
gen xxol = mfooopt
replace xxoh = . if highgroup ==0
replace xxol = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)
egen bazol = max(xxol)
egen bazoh = max(xxoh)

local mfool = bazl
local mfooh = bazh
local mfoolopt = bazol
local mfoohopt = bazoh


cumul alpha_i if highgroup ==1, gen(cu_h)
cumul alpha_i if highgroup ==0, gen(cu_l)
sort alpha_opt_i highgroup
cumul alpha_opt_i if highgroup ==1, gen(cu_h_o)
cumul alpha_opt_i if highgroup ==0, gen(cu_l_o)

line cu_h alpha_i if highgroup ==1 & alpha_i >lb & alpha_i < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l alpha_i if highgroup ==0 & alpha_i >lb & alpha_i < hb, sort(cu_l) lcolor(dkgreen)	///
	|| line cu_h_o alpha_opt_i if highgroup ==1 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_h_o) lpattern(dash) lcolor(dkorange)	///
	|| line cu_l_o alpha_opt_i if highgroup ==0 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_l_o) lpattern(dash) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
					xline(`mfoolopt', lcolor(dkgreen) lpattern(dash)) ///
		xline(`mfoohopt', lcolor(dkorange) lpattern(dash)) ///
	t1title("Baseline" , size(medium)) xtitle("{&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)1) ///
	ytitle("Cdf", size(small)) ///
	  saving("$tempgraph/bl", replace) 
	  
restore
preserve
keep if treatmentname == "OneGroup"
sort alpha_i highgroup
egen mfoo = mean(alpha_i), by(highgroup)
egen mfooopt = mean(alpha_opt_i), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
gen xxoh = mfooopt
gen xxol = mfooopt
replace xxoh = . if highgroup ==0
replace xxol = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)
egen bazol = max(xxol)
egen bazoh = max(xxoh)

local mfool = bazl
local mfooh = bazh
local mfoolopt = bazol
local mfoohopt = bazoh


cumul alpha_i if highgroup ==1, gen(cu_h)
cumul alpha_i if highgroup ==0, gen(cu_l)
sort alpha_opt_i highgroup
cumul alpha_opt_i if highgroup ==1, gen(cu_h_o)
cumul alpha_opt_i if highgroup ==0, gen(cu_l_o)

line cu_h alpha_i if highgroup ==1 & alpha_i >lb & alpha_i < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l alpha_i if highgroup ==0 & alpha_i >lb & alpha_i < hb, sort(cu_l) lcolor(dkgreen)	///
	|| line cu_h_o alpha_opt_i if highgroup ==1 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_h_o) lpattern(dash) lcolor(dkorange)	///
	|| line cu_l_o alpha_opt_i if highgroup ==0 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_l_o) lpattern(dash) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
					xline(`mfoolopt', lcolor(dkgreen) lpattern(dash)) ///
		xline(`mfoohopt', lcolor(dkorange) lpattern(dash)) ///
	t1title("OneGroup" , size(medium)) xtitle("{&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)1) ///
	ytitle("Cdf", size(small)) ///
	  saving("$tempgraph/og", replace) 
	  
	  restore
preserve
keep if treatmentname == "NoGroup"
sort alpha_i highgroup
egen mfoo = mean(alpha_i), by(highgroup)
egen mfooopt = mean(alpha_opt_i), by(highgroup)
gen xxh = mfoo
gen xxl = mfoo
replace xxh = . if highgroup ==0
replace xxl = . if highgroup ==1
gen xxoh = mfooopt
gen xxol = mfooopt
replace xxoh = . if highgroup ==0
replace xxol = . if highgroup ==1
egen bazl = max(xxl)
egen bazh = max(xxh)
egen bazol = max(xxol)
egen bazoh = max(xxoh)

local mfool = bazl
local mfooh = bazh
local mfoolopt = bazol
local mfoohopt = bazoh


cumul alpha_i if highgroup ==1, gen(cu_h)
cumul alpha_i if highgroup ==0, gen(cu_l)
sort alpha_opt_i highgroup
cumul alpha_opt_i if highgroup ==1, gen(cu_h_o)
cumul alpha_opt_i if highgroup ==0, gen(cu_l_o)

line cu_h alpha_i if highgroup ==1 & alpha_i >lb & alpha_i < hb, sort(cu_h) lcolor(dkorange) ///
	|| line cu_l alpha_i if highgroup ==0 & alpha_i >lb & alpha_i < hb, sort(cu_l) lcolor(dkgreen)	///
	|| line cu_h_o alpha_opt_i if highgroup ==1 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_h_o) lpattern(dash) lcolor(dkorange)	///
	|| line cu_l_o alpha_opt_i if highgroup ==0 & alpha_opt_i >lb & alpha_opt_i < hb, sort(cu_l_o) lpattern(dash) lcolor(dkgreen)	///
				xline(`mfool', lcolor(dkgreen) lpattern(solid)) ///
		xline(`mfooh', lcolor(dkorange) lpattern(solid)) ///
					xline(`mfoolopt', lcolor(dkgreen) lpattern(dash)) ///
		xline(`mfoohopt', lcolor(dkorange) lpattern(dash)) ///
	t1title("NoGroup" , size(medium)) xtitle("{&omega}", size(medium)) graphregion(color(white)) bgcolor(white) ///
	legend(off) ///
	xlabel(-0.2(0.2)1) ///
	ytitle("Cdf", size(small)) ///
	  saving("$tempgraph/ng", replace) 
	  
	  
graph combine "$tempgraph/bl" "$tempgraph/ng" "$tempgraph/og" "$tempgraph/sf", graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(2) title("") ///
title("", size(medium)) 

/*
note("Green (Orage) solid line represents estimated weight on prior for low-mean (high-mean) group." ///
"Green (Orage) dashed line represents Bayesian weight on prior for low-mean (high-mean) group." ///
"Vertical lines denote median values.", size(vsmall))  
*/
graph export "$text/files/cdfofalphas_late.pdf", replace


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// FIGURE 12 Accuracy-Discrimination Tradeoff (Using Median Values)

See code from Figures_Final


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// FIGURE 13 Distribution of Estimated Signal Error by Treatment

use "$data/eoydata_late.dta", clear
keep if filteredsample ==1
gen syerror = signal -value

keep if syerror >-31 & syerror <31

preserve
keep if treatmentname == "Baseline"

hist syerror, percent bin(100) ylabel(0(2)10) ///
xlabel(-30(10)30)  normal ///
xtitle("Predicted Signal- True Type" , size(small)) ///
graphregion(color(white)) bgcolor(white) ///
subtitle("Baseline") ///
saving("$tempgraph/bl", replace)
restore

preserve
keep if treatmentname == "SignalFirst"
hist syerror, percent bin(100) ylabel(0(2)8) ///
xlabel(-30(10)30)  normal ///
xtitle("Predicted Signal- True Type" , size(small)) ///
graphregion(color(white)) bgcolor(white) ///
subtitle("SignalFirst") ///
saving("$tempgraph/sf", replace)
restore

preserve
keep if treatmentname == "OneGroup"
hist syerror, percent bin(100) ylabel(0(2)8) ///
xlabel(-30(10)30)  normal ///
xtitle("Predicted Signal- True Type" , size(small)) ///
graphregion(color(white)) bgcolor(white) ///
subtitle("OneGroup") ///
saving("$tempgraph/og", replace)
restore

preserve
keep if treatmentname == "NoGroup"
hist syerror, percent bin(100) ylabel(0(2)8) ///
xlabel(-30(10)30)  normal ///
xtitle("Predicted Signal- True Type" , size(small)) ///
graphregion(color(white)) bgcolor(white) ///
subtitle("NoGroup") ///
saving("$tempgraph/ng", replace)
restore

graph combine "$tempgraph/bl" "$tempgraph/ng" "$tempgraph/og" "$tempgraph/sf", graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(2) title("") ///
title("", size(medium)) ///
note("Yellow bars depict empirical histogram. Green line shows normal fit." ///
"" ///
"", size(vsmall))  

graph export "$text/files/errornormal_late.pdf", replace


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// FIGURE 14 Average Actual and Bayesian Assessment by True Type in Each Treatment
/// GUESS BY VALUE WITH BAYESIAN BENCHMARK
************************************
use "$data/eoydata_late.dta", clear
tab treatmentname bias if highgroup ==1
tab treatmentname bias if highgroup ==0
tab treatmentname alpha if highgroup ==1
tab treatmentname alpha if highgroup ==0
keep if filteredsample ==1
gen predictguess = blow + alpha*mean_theory + (1- alpha)*value + highgroup*bdelta
gen predictguess_bay = alpha_opt*mean_theory + (1- alpha_opt)*value
gen lbound = 30
gen hbound = 70
gen dummy = 1
egen weight = sum(dummy), by(value highgroup treatmentname)
collapse guess predictguess  predictguess_bay lbound hbound weight, by(value highgroup treatmentname)

preserve
keep if treatmentname =="Baseline"

tw scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==1 ,  msize(*0.15) mcolor(orange*0.5) msymbol(oh)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen)  ||   ////
line predictguess_bay value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange) lwidth(thick) || ///
line predictguess_bay value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen) lwidth(thick) ||   ////
scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==0 ,  msize(*0.15) mcolor(green*0.5) msymbol(oh) ||  ///
rcap predictguess value value if value ==40 & highgroup ==0 , lcolor(dkgreen) msymbol(oh)  || ///
rcap predictguess value value if value ==60 & highgroup ==1 , lcolor(dkorange) msymbol(oh)   ///
xtitle("Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
  text(28 40 "B = -1.7, {&omega} = 0.20", color(dkgreen) size(small)) ///
  text(70 55 "B = 1.8, {&omega} = 0.16", color(dkorange) size(small)) ///
 title("Baseline", size(medsmall) ) ///
  saving("$tempgraph/bl", replace)
  
  
   
  restore
  preserve
keep if treatmentname =="SignalFirst"

tw scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==1 ,  msize(*0.15) mcolor(orange*0.5) msymbol(oh)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen)  ||   ////
line predictguess_bay value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange) lwidth(thick) || ///
line predictguess_bay value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen) lwidth(thick) ||   ////
scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==0 ,  msize(*0.15) mcolor(green*0.5) msymbol(oh) ||  ///
rcap predictguess value value if value ==40 & highgroup ==0 , lcolor(dkgreen) msymbol(oh)  || ///
rcap predictguess value value if value ==60 & highgroup ==1 , lcolor(dkorange) msymbol(oh)   ///
xtitle("Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
  text(28 40 "Bias = -0.3, {&omega} = 0.18", color(dkgreen) size(small)) ///
  text(70 55 "Bias = 0.2, {&omega} = 0.15", color(dkorange) size(small)) ///
 title("SignalFist", size(medsmall) ) ///
  saving("$tempgraph/sf", replace) 
  
  restore
  preserve
keep if treatmentname =="OneGroup"

tw scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==1 ,  msize(*0.15) mcolor(orange*0.5) msymbol(oh)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen)  ||   ////
line predictguess_bay value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange) lwidth(thick) || ///
line predictguess_bay value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen) lwidth(thick) ||   ////
scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==0 ,  msize(*0.15) mcolor(green*0.5) msymbol(oh) ||  ///
rcap predictguess value value if value ==40 & highgroup ==0 , lcolor(dkgreen) msymbol(oh)  || ///
rcap predictguess value value if value ==60 & highgroup ==1 , lcolor(dkorange) msymbol(oh)   ///
xtitle("Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
  text(28 40 "Bias = 0.4, {&omega} = 0.09", color(dkgreen) size(small)) ///
  text(70 55 "Bias = -0.2, {&omega} = 0.16", color(dkorange) size(small)) ///
 title("OneGroup", size(medsmall) ) ///
  saving("$tempgraph/og", replace) 
  
  restore
  preserve
keep if treatmentname =="NoGroup"

tw scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==1 ,  msize(*0.15) mcolor(orange*0.5) msymbol(oh)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange)  || ///
line predictguess value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen)  ||   ////
line predictguess_bay value if value >lbound & value < hbound & highgroup ==1 , lcolor(dkorange) lwidth(thick) || ///
line predictguess_bay value if value >lbound & value < hbound & highgroup ==0 , lcolor(dkgreen) lwidth(thick) ||   ////
scatter guess value [w=weight] if value >lbound & value < hbound & highgroup ==0 ,  msize(*0.15) mcolor(green*0.5) msymbol(oh) ||  ///
rcap predictguess value value if value ==40 & highgroup ==0 , lcolor(dkgreen) msymbol(oh)  || ///
rcap predictguess value value if value ==60 & highgroup ==1 , lcolor(dkorange) msymbol(oh)   ///
xtitle("Type", size(small)) ytitle("Mean Assesment", size(small))  ///s
ylabel(25 (10) 75) ///
 legend(off) graphregion(color(white)) bgcolor(white) ///
  text(28 40 "Bias = -0.3, {&omega} = 0.02", color(dkgreen) size(small)) ///
  text(70 55 "Bias = -0.1, {&omega} = 0.02", color(dkorange) size(small)) ///
 title("NoGroup", size(medsmall) ) ///
  saving("$tempgraph/ng", replace) 
 
 
  
       graph combine "$tempgraph/bl"  "$tempgraph/ng" , graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(1) title("Finite") ///
title("", size(medium)) ///
  note("" ///
 , size(tiny))  
graph export "$text/files/guessbyvalue_blng_withbay_late.pdf", replace

   
       graph combine "$tempgraph/og"  "$tempgraph/sf" , graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(1) title("Finite") ///
title("", size(medium)) ///
  note("Green (Orange) dots are for low (high) mean group. Thinner Green and Orange lines depict best linear fit by group and treatment." ///
  "Thicker Green and Orange lines depict outcome of Bayesian inference strategy by group and treatment; gray line depicts 45 degree line. Subjects with MSE > 200 excluded." ///
 , size(tiny))  
graph export "$text/files/guessbyvalue_ogsf_withbay_late.pdf", replace


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// FIGURE 15 Mean Assessment by Type with Efficient Coding ($n = 10$)
/// FIGURE 16 Mean Assessment by Type with Efficient Coding ($n = 20$)

import delimited "$m/output/ecsoln_wsearch_20.txt", clear //
import delimited "$m/output/ecsoln_wsearch_10.txt", clear //
rename v1 NoGroup
rename v2 OneGroup
rename v3 SignalFirst
gen value = _n
gen xxvalue = 100-value

gen OneGroup_low = -999
gen SignalFirst_low = -999

forvalues i=1/100 {
	gen dummy = xxvalue == `i'
	gen foo_og = dummy*OneGroup
	egen baz_og = max(foo_og)
	replace OneGroup_low =  value + (xxvalue - baz_og) if value ==`i'
	gen foo_sf = dummy*SignalFirst
	egen baz_sf = max(foo_sf)
	replace SignalFirst_low =  value + (xxvalue - baz_sf) if value ==`i'
	drop dummy foo* baz*	
}

gen lbound = 30
gen hbound = 70

gen guess = NoGroup

keep if value > lbound-1
keep if value < hbound+1




tw ///
lowess OneGroup value , lcolor(dkorange) || ///
lowess OneGroup_low value , lcolor(dkgreen) msymbol(oh)  || ///
line value value, lcolor(gray*0.7)    ////
////
xtitle("Type", size(small)) ytitle("Mean Assesment", size(small))  ///
ylabel(30 (10) 70) xlabel(30 (10) 70) //////
legend(off)  ///
graphregion(color(white)) bgcolor(white) ///
 title("OneGroup", size(medsmall) )  aspect(1.8) ///
  saving("$tempgraph/og", replace) 
  
  
 tw ///
lowess SignalFirst value , lcolor(dkorange)  || ///
lowess SignalFirst_low value , lcolor(dkgreen) msymbol(oh)  || ///
line value value, lcolor(gray*0.7)    ////
////
xtitle("Type", size(small)) ytitle("Mean Assesment", size(small))  ///
ylabel(30 (10) 70) xlabel(30 (10) 70) //////
legend(off)  ///
graphregion(color(white)) bgcolor(white) ///
 title("SignalFirst", size(medsmall) )  aspect(1.8) ///
  saving("$tempgraph/sf", replace) 
  
  
   tw ///
lowess NoGroup value , lcolor(dkblue)  || ///
line value value, lcolor(gray*0.7)    ////
////
xtitle("Type", size(small)) ytitle("Mean Assesment", size(small))  ///
ylabel(30 (10) 70) xlabel(30 (10) 70) //////
legend(off)  ///
graphregion(color(white)) bgcolor(white) ///
 title("NoGroup", size(medsmall) ) aspect(1.8) ///
  saving("$tempgraph/ng", replace) 
   
  
         graph combine "$tempgraph/og"  "$tempgraph/sf" "$tempgraph/ng" , graphregion(color(white)) ///
scheme(s2mono) plotregion(fcolor(white)) row(1) title("Finite") ///
title("", size(medium)) ///
  note("" ///
 , size(tiny))  
 
 
graph export "$overleaf/efficientcoding.pdf", replace
graph export "$overleaf/efficientcoding_20.pdf", replace



